#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 第1行注释可以让这个hello.py文件直接在Unix/Linux/Mac上运行，
# 第2行注释表示.py文件本身使用标准UTF-8编码：
import requests
import re

url = ('https://image.baidu.com/search/flip?'
       'tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1542554458266_R&pv=&ic=0&nc=1&z=&se'
       '=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1542554458266%5E00_1349X611&word=美女')

r = requests.get(url)  # <Response [200]>
img_list = re.findall(r'"objURL":"(.*?)"', r.content.decode())
for i in img_list:
    pic_url = re.search(r'(jpg)|(jpeg)|(png)|(gif)$', i)
    if pic_url is None:
        pic_url += '.gif'
    path = i[-10:]
    try:
        with open('img/%s' % path, 'ab') as f:
            r = requests.get(i, timeout=3)
            f.write(r.content)
    except Exception as e:
        print(e)
        pass
